<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-row>
            <el-button type="primary" size="small" @click="onAdd">新增角色</el-button>
          </el-row>
          <el-table :data="list" border stripe style="width:100%,margin-top:15px">
            <el-table-column label="序号" type="index" width="80"></el-table-column>
            <el-table-column label="角色" prop="name"></el-table-column>
            <el-table-column label="描述" prop="description"></el-table-column>
            <el-table-column label="操作">
              <template v-slot="{row}">
                <el-button type="sucess" size="small">分配权限</el-button>
                <el-button type="primary" size="small" @click="updateRole(row.id)">编辑</el-button>
                <el-button type="danger" size="small" @click="deleteRole(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row type="flex" justify="end" style="margin-top:15px">
            <el-pagination
              layout="prev,pager,next"
              :page-size="page.pagesize"
              :total="page.total"
              @current-change="onCurrentChange"
            ></el-pagination>

          </el-row>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            show-icon
          ></el-alert>
          <el-form label-width="80px" style="width: 500px; margin-top: 15px">
            <el-form-item label="公司名称">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="form.companyAddress"></el-input>
            </el-form-item>
            <el-form-item label="公司邮箱">
              <el-input v-model="form.mailbox"></el-input>
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="form.remarks" disabled type="textarea"></el-input>
            </el-form-item>
          </el-form></el-tab-pane>
      </el-tabs>

      <!-- el-form>(el-form-item[label]>el-input[v-model="form.xxx" :disabled="true"]{})*4 -->
    </div>
    <el-dialog :visible="showDialog" v-bind="$attrs" :title="formDate.id?'编辑角色':'新建角色'" v-on="$listeners" @open="onOpen" @close="close">
      <el-form ref="formRef" :model="formDate" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model.trim="formDate.name" placeholder="请输入角色名称" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="formDate.description" placeholder="请输入角色描述" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="felx" justify="center">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm()">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getCompanyById, getRoles, addRole, updateRole, deleteRole, getRoleDetail } from '../../api/setting.js'
export default {
  name: 'SettingPage',
  components: {},
  props: {},
  data() {
    return {
      showDialog: false,
      form: {},
      list: [],
      page: {
        page: 1,
        pagesize: 10,
        total: 0
      },
      formDate: {
        name: undefined,
        description: undefined
      },
      rules: {
        name: [{
          required: true,
          message: '请输入角色名称',
          trigger: 'blur'
        }],
        description: []
      }
    }
  },
  async created() {
    this.form = await getCompanyById(this.$store.state.user.userInfo.companyId)
    this.getRoles()
  },
  mounted() { },
  methods: {
    onCurrentChange(val) {
      this.page.page = val
      this.getRoles()
    },
    async getRoles() {
      const { rows, total } = await getRoles(this.page)
      this.list = rows
      this.page.total = total
    },
    onAdd() { this.showDialog = true },
    onOpen() {},

    close() {
      this.$refs['formRef'].resetFields()
      this.showDialog = false
      this.formDate = {
        name: undefined,
        description: undefined
      }
      this.showDialog = false
    },
    handelConfirm() {
      this.$refs['formRef'].validate(async valid => {
        if (!valid) return
        if (this.formDate.id) {
          await updateRole(this.formDate)
        } else {
          await addRole(this.formDate)
        }
        this.$message('操作成功')
        this.getRoles()
        this.close()
      })
    },
    async updateRole(id) {
      this.formDate = await getRoleDetail(id)
      this.showDialog = true
    },
    deleteRole(id) {
      this.$confirm('确认删除？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        await deleteRole(id)
        this.$message('删除成功')
        this.getRoles()
      }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

